/*
4_SecondStage.do

Creates all second stage output tables


"Human Capital and Climate Change", 
	by Noam Angrist, Kevin Winseck, Harry Patrinos, and Joshua Graff Zivin.
		
Inputs:
	Cleaned_ESS_withFirstStage.dta
Outputs:
	Table2PanelA.tex
	Table2PanelB.tex
	Table3PanelA.tex
	Table3PanelB.tex
	Table3PanelC.tex
	SecondStage_GreenVoting_byCountry.tex (Appendix Table A3)
	

*/

**************************** Set up data ****************************
global clustervar "cntryXlaw"

use "Cleaned_ESS_withFirstStage.dta", clear

drop if mi(minEdu)
* Drop if missing CSL data



* Relabel some outcomes for tables
label var green_recoded "Vote Green"
label var lrscale_proenv "Left-leaning voting"
label var lrscale_recoded "Left-leaning voting"

label var eduyrs_wins "Years of education"




* Instrument(s) to use in 2SLS--------------------------------------------------
local instrument "reform_pos_sig_* reform_pos_only_* reform_only_*" /*all reforms, main paper specification*/
*local instrument "reform_pos_sig_* reform_pos_only_*"  /*positive (and pos+sig) reforms*/
*local instrument "reform_pos_sig_*" /* positive significant reforms only, main paper specification*/
*local instrument "b2.cntry_numeric#c.minEdu" /*contemporaneous level of minimum required schooling interacted with country*/

* Time trend to condition on----------------------------------------------------
*local time_trend ""
*local time_trend_lab "None"
local time_trend "yrbrn"
local time_trend_lab "Pooled Linear"
*local time_trend "b2.cntry_numeric#c.yr" /*main paper specification*/
*local time_trend_lab "Linear by Country"
*local time_trend "i.cntry_numeric#c.yr i.cntry_numeric#c.yr2"
*local time_trend_lab "Quadratic by Country"

* Additional conditions to pass to regressions
*	 other than in_sample==1 -----------------------------------------------
local cond "& in_pooled==1" /*in sample of all countries with at least one CSL change in the data*/
*local cond "& in_pos==1" /*in sample of positive first stages*/
*local cond "& in_pos_sig==1" /*in sample of positive and significant first stages, main paper specification*/
*local cond "& in_pooled==1" /*in sample of all countries with at least one CSL change in the data, same condition as above, to be used with minEdu instrument*/


*ivregress 2sls green_proenv_with0s (eduyrs_wins=`instrument') /*`time_trend'*/ i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar)
*aggregate_estimates green_proenv_with0s if in_sample==1 `cond'
*ivregress 2sls voting_proenv (eduyrs_wins=`instrument') i.yrbrn i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar)
*ivregress 2sls voting_proenv (eduyrs_wins=`instrument') i.yrbrn i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar)


* Create program to estimate aggregates by country. See paper for explanation of methodology

cap program drop aggregate_estimates
program aggregate_estimates, rclass
	syntax varlist [if]
	args outcome
	
	gen estimate = .
	gen weight = .
	
	levelsof cntry, local(countries)

	foreach country in `countries'{
		cap ivregress 2sls `outcome' (eduyrs_wins=reform_*_`country'*) yr if in_sample==1 & cntry=="`country'" & in_pooled==1
		if _rc==0{
			local beta = e(b)[1,1]
			replace estimate = `beta' if cntry=="`country'"
			local variance = e(V)[1,1]
			replace weight = 1/`variance' if cntry=="`country'"
			}
			
	}

	bys cntry_numeric: gen first_in_country = _n==1
	replace estimate = . if first_in_country!=1
	replace weight = . if first_in_country!=1

	egen total_weight = total(weight)
	replace weight = weight/total_weight
	gen weighted_estimate = estimate*weight
	egen result = total(weighted_estimate)
	su result
	local to_return = r(mean)

	drop estimate weight total_weight weighted_estimate result first_in_country
		
	return scalar beta=`to_return'

end



eststo: ivregress 2sls voting_proenv (eduyrs_wins=reform_pos_sig_* reform_pos_only_* reform_only_*) yrbrn i.cntry_numeric if in_sample==1 & in_pooled==1, vce(cluster $clustervar)
eststo: ivregress 2sls voting_proenv (eduyrs_wins=reform_pos_sig_* reform_pos_only_* reform_only_*) yrbrn i.cntry_numeric i.essround if in_sample==1 & in_pooled==1, vce(cluster $clustervar)





********************************************************************************
****** Table 2 Outcome Indices with Linear Time Trends
********************************************************************************

* Panel A: above-median indicators
eststo clear

foreach outcome in beliefs_proenv behaviors_proenv policy_proenv voting_proenv {
	aggregate_estimates `outcome' if in_sample==1 & in_pooled==1
	local aggregate_estimate: display%9.3f r(beta)
	
	eststo: ivregress 2sls `outcome' (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar)
	gen in_reg_sample=e(sample)
	local nclust =  e(N_clust)
	local beta = _b[eduyrs_wins]
	su `outcome' if in_reg_sample==1
	local thismean: display%9.3f r(mean)
	local pct: display%9.1f `beta' / r(mean) * 100
	local fouryears: display%9.1f `beta' * 4 * 100
	drop in_reg_sample
	estadd local Clusters "`nclust'"
	estadd local Mean = "`thismean'"
	estadd local PercentChange = "`pct' \%"
	estadd local AggregateEstimate = "`aggregate_estimate'"
	
	
}

esttab using "Table2PanelA.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) scalars(AggregateEstimate Clusters Mean PercentChange) keep(eduyrs_wins) label nostar nonote

* Panel B: continuous outcomes
eststo clear

foreach outcome in beliefs_recoded behaviors_recoded policy_recoded voting_recoded {
	aggregate_estimates `outcome' if in_sample==1 & in_pooled==1
	local aggregate_estimate: display%9.3f r(beta)
	
	eststo: ivregress 2sls `outcome' (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar)
	gen in_reg_sample=e(sample)
	local nclust =  e(N_clust)
	local beta = _b[eduyrs_wins]
	su `outcome' if in_reg_sample==1
	local thismean: display%9.3f r(mean)
	local pct: display%9.1f `beta' / r(mean) * 100
	local fouryears: display%9.1f `beta' * 4 * 100
	drop in_reg_sample
	estadd local Clusters "`nclust'"
	estadd local Mean = "`thismean'"
	estadd local PercentChange = "`pct' \%"
	estadd local AggregateEstimate = "`aggregate_estimate'"
	
	
}

esttab using "Table2PanelB.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) scalars(AggregateEstimate Clusters Mean PercentChange) keep(eduyrs_wins) label nostar nonote


********************************************************************************
****** Table 3 Panel A: Beliefs
********************************************************************************
eststo clear

foreach outcome in clmchng ccgdbd wrclmch energy wrdpfos {
	aggregate_estimates `outcome'_proenv if in_sample==1 & in_pooled==1
	local aggregate_estimate: display%9.3f r(beta)
	
	eststo: ivregress 2sls `outcome'_proenv (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar )
	local nclust =  e(N_clust) 
	gen in_reg_sample=e(sample)
	local beta = _b[eduyrs_wins]
	su `outcome'_proenv if in_reg_sample==1
	local thismean: display%9.3f r(mean)
	local pct: display%9.1f `beta' / r(mean) * 100
	drop in_reg_sample
	estadd local Clusters "`nclust'"
	estadd local Mean = "`thismean'"
	estadd local PercentChange = "`pct' \%"
	estadd local AggregateEstimate = "`aggregate_estimate'"
}
esttab using "Table3PanelA.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) scalars(AggregateEstimate Clusters Mean PercentChange) keep(eduyrs_wins) label nostar nonote

********************************************************************************
****** Table 3 Panel B: Behaviors
********************************************************************************
eststo clear

foreach outcome in  clmthgt eneffap rdcenr {
	aggregate_estimates `outcome'_proenv if in_sample==1 & in_pooled==1
	local aggregate_estimate: display%9.3f r(beta)
	
	eststo: ivregress 2sls `outcome'_proenv (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar )
	local nclust =  e(N_clust) 
	gen in_reg_sample=e(sample)
	local beta = _b[eduyrs_wins]
	su `outcome'_proenv if in_reg_sample==1
	local thismean: display%9.3f r(mean)
	local pct: display%9.1f `beta' / r(mean) * 100
	drop in_reg_sample
	estadd local Clusters "`nclust'"
	estadd local Mean = "`thismean'"
	estadd local PercentChange = "`pct' \%"
	estadd local AggregateEstimate = "`aggregate_estimate'"
}
esttab using "Table3PanelB.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) scalars(AggregateEstimate Clusters Mean PercentChange) keep(eduyrs_wins) label nostar nonote


********************************************************************************
****** Table 3 Panel C: Policy & Voting
********************************************************************************
eststo clear

foreach outcome in  inctxff sbsrnen banhhap green{ /*Here vote green is in this table but not part of the policy index!*/
	aggregate_estimates `outcome'_proenv if in_sample==1 & in_pooled==1
	local aggregate_estimate: display%9.3f r(beta)
	
	eststo: ivregress 2sls `outcome'_proenv (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond', vce(cluster $clustervar )
	local nclust =  e(N_clust) 
	gen in_reg_sample=e(sample)
	local beta = _b[eduyrs_wins]
	su `outcome'_proenv if in_reg_sample==1
	local thismean: display%9.3f r(mean)
	local pct: display%9.1f `beta' / r(mean) * 100
	drop in_reg_sample
	estadd local Clusters "`nclust'"
	estadd local Mean = "`thismean'"
	estadd local PercentChange = "`pct' \%"
	estadd local AggregateEstimate = "`aggregate_estimate'"
}
esttab using "Table3PanelC.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) scalars(AggregateEstimate Clusters Mean PercentChange) keep(eduyrs_wins) label nostar nonote






********************************************************************************
***** Appendix Table A3: Green Voting by Country
********************************************************************************
eststo clear
eststo: ivregress 2sls green_proenv (c.eduyrs_wins#i.cntry_numeric=reform_pos_sig_* reform_pos_only_* reform_only_*) yrbrn i.cntry_numeric if in_sample==1 & in_pooled==1, vce(cluster $clustervar)
esttab using "SecondStage_GreenVoting_byCountry.tex", drop(*cntry_numeric yrbrn _cons) noomit label cells(`"b(fmt(3)) se(par("(" ")") fmt(3)) p(par("[" "]") fmt(3))"') nostar nonote replace

